/*
 * @Author: lirl
 * @Date: 2020-11-13 10:22:52
 * @LastEditTime: 2024-01-16 19:27:02
 * @Description: file content
 */
// 信息提示框
import { MessageBox, Message } from 'element-ui';
import { window } from 'globalthis/implementation';
import {
  alertErrorMsg as alertErrorMessage,
  message as infoMessage,
  messageAlert as infoMessageAlert,
  messageConfirm as infoMessageConfirm,
  messageWarning as infoMessageWarning,
  showErrorMsg as showErrorMessage,
  messageTipConfirm as infoMessageTipConfirm,
  messagePrompt as infoMessagePrompt
} from 'jdf-util';
import { isObject } from 'lodash-es';
import { collectLogs } from 'jdf-jssdk';
import { ssoLoginPageURL, tokenCodes } from './constants';

/*
 * @Author: lirl
 * @Date: 2023-04-02 18:55:05
 * @Description: 提示信息
 */
const message = infoMessage(Message);
const messageAlert = infoMessageAlert(MessageBox.alert);
const messageConfirm = infoMessageConfirm(MessageBox.confirm);

/**
 * MessageBox.prompt
 * @param {Object} config 配置信息
 * @param {String} config.tooltip 提示信息
 * @param {String} config.title messageBox的标题
 * @param {Boolean} config.isRequired 是否必填
 * @param {String} config.validateInfo 输入框校验不通过时的提示信息
 * @param {Function} yesCallback 点击确定按钮的回调方法
 * @param {Function} cancelCallback 点击取消按钮的回调方法
 */
const messagePrompt = infoMessagePrompt(MessageBox.prompt);

const validateMessage = (text) => message(text, 'warning', 3000, true, 'pj-validate');
/**
 * 弹出确认框.
 * @param {String} content 提示内容
 * @param {Function} yes 点击确定按钮的回调
 * @param {Function} cancel 点击取消按钮的回调
 * @returns Promise 调用方式： mmessageConfirm("提示信息", () => {
 *  alert('确定');
 * }, () => {
 *  alert('取消');
 * })
 */
const messageTipConfirm = infoMessageTipConfirm(MessageBox.confirm);

/**
 * 展示错误信息
 * @param {*} error
 */
const showErrorMsg = (error) => {
  if (tokenCodes.includes(error?.code)) {
    window.open(ssoLoginPageURL, '_self');
  } else {
    showErrorMessage(Message)(error);
  }
};

/**
 * 弹出错误信息
 * @param {} error
 */
const alertErrorMsg = (error) => {
  if (tokenCodes.includes(error?.code)) {
    window.open(ssoLoginPageURL, '_self');
  } else {
    if (isObject(error) && error.stack) {
      collectLogs({ msg: JSON.stringify(error.stack) }, window.location.href);
    }
    alertErrorMessage(MessageBox.alert)(error);
  }
};

/**
 * 抢办时的校验
 * @param {} param
 */
const messageWarning = infoMessageWarning(Message);

export {
  messageWarning,
  alertErrorMsg,
  showErrorMsg,
  messageTipConfirm,
  messageConfirm,
  message,
  messageAlert,
  messagePrompt,
  validateMessage,
};
